package com.echat.messagesender.sender.sms.impl;

import com.echat.messagesender.sender.exception.EchatSmsException;
import com.echat.messagesender.sender.sms.SmsSender;
import com.echat.messagesender.sender.sms.bean.AliSmsConfig;
import com.echat.messagesender.sender.bean.EchatSendSmsInfo;
import com.echat.messagesender.sender.sms.impl.baseutil.AliSmsUtil;
import com.echat.messagesender.sender.util.JsonUtil;
import com.echat.messagesender.sender.util.StringUtils;

import java.util.Map;

/**
 * Created by chenmohan on 2018/08/16
 *
 * @description
 */
public class AliSmsSender extends SmsSender {

    private AliSmsConfig config;

    public AliSmsSender(AliSmsConfig config) {
        config.check();
        this.config = config;
        super.platformAccountId = config.getPlatformAccountId();
        super.platformName = config.getPlatformName();
    }

    @Override
    public boolean sendSms(EchatSendSmsInfo info) {
        String[] phonoNos = info.getPhonoNos();
        for (String phonoNo : phonoNos) {
            if (StringUtils.isNotEmpty(info.getTemplateId())) {
                //使用短信模板发送
                try {
                    return AliSmsUtil.sendTemplateSMS(config, phonoNo, info.getTemplateId(), JsonUtil.toMap(info.getVars()));
                } catch (Exception e) {
                    return false;
                }
            } else {
                //费短信模板发送
                sendSms(phonoNo, info.getSmsContent());
            }
        }
        return true;

    }

    @Override
    public boolean sendBatchSms(String[] receiveNos, String templateId, Map<String, String>[] templateParams) {
        return AliSmsUtil.sendBatchTemplateSMS(config, receiveNos, templateId, templateParams);
    }

    @Override
    public boolean sendSms(String receiveNos, String content) {
        throw new EchatSmsException("AliSMS的API中必须使用模板，因此暂不支持调用此方法 AliSmsSender.sendSms(String receiveNos, String content) " +
                "uri:https://help.aliyun.com/document_detail/55284.html?spm=a2c4g.11186623.6.557.FAJP8N");
    }
}
